package com.egao.common.module.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.DispatchCompany;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.mapper.DispatchCompanyMapper;
import com.egao.common.module.student.service.DispatchCompanyService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 派遣单位服务接口实现类
 *
 * @author lym
 */
@Service
public class DispatchCompanyServiceImpl extends ServiceImpl<DispatchCompanyMapper, DispatchCompany> implements DispatchCompanyService {

    @Autowired
    private DictService dictService;

    @Override
    public DispatchCompany getByCompanyCode(String companyCode) {
        return baseMapper.selectOne(new QueryWrapper<DispatchCompany>()
        .eq("company_code", companyCode));
    }

    @Override
    public JsonResult getDispatchByCode(StudentSign studentSign, String code) {
        Dict dict = dictService.getByTypeAndValue(DictConstants.TYPE_SYSZD, code);
        if (dict == null) {
            return JsonResult.error("学生生源地区异常");
        }

        JsonResult ok = JsonResult.ok();

        // 特殊 神农架林区人才中心
        if ("429021".equals(code)) {
            studentSign.setDispatchCompanyCode("429200001");
            setStudentSignByDispatchCompany(studentSign, getByCompanyCode("429200001"));
            return ok;
        }

        // 特殊 仙桃市人才服务中心
        if ("429004".equals(code)) {
            studentSign.setDispatchCompanyCode("429400001");
            setStudentSignByDispatchCompany(studentSign, getByCompanyCode("429400001"));
            return ok;
        }

        // 特殊 潜江市大中专毕业生就业指导服务中心
        if ("429005".equals(code)) {
            studentSign.setDispatchCompanyCode("429500001");
            setStudentSignByDispatchCompany(studentSign, getByCompanyCode("429500001"));
            return ok;
        }

        // 特殊 天门市人才服务局
        if ("429006".equals(code)) {
            studentSign.setDispatchCompanyCode("429600001");
            setStudentSignByDispatchCompany(studentSign, getByCompanyCode("429600001"));
            return ok;
        }

        // 单位代码组成：湖北省内=生源地区代码+001 湖北省外=09+生源地区代码+1
        String companyCode = StrUtil.contains(dict.getLabel(), StudentSignConstants.HB) ? code + "001" : "09" + code + "1";

        DispatchCompany first = getByCompanyCode(companyCode);
        if (first != null) {
            studentSign.setDispatchCompanyCode(companyCode);
            setStudentSignByDispatchCompany(studentSign, first);
            return ok;
        }

        companyCode = StrUtil.replace(companyCode, 6, 8, "0".charAt(0));
        DispatchCompany second = getByCompanyCode(companyCode);
        if (second != null) {
            studentSign.setDispatchCompanyCode(companyCode);
            setStudentSignByDispatchCompany(studentSign, second);
            return ok;
        }

        companyCode = StrUtil.replace(companyCode, 4, 6, "0".charAt(0));
        DispatchCompany last = getByCompanyCode(companyCode);
        if (last != null) {
            studentSign.setDispatchCompanyCode(companyCode);
            setStudentSignByDispatchCompany(studentSign, last);
            return ok;
        }

        if (StrUtil.isBlank(studentSign.getDispatchReportCompanyProvince())) {
            return JsonResult.error("没有找到单位所属省份");
        }
        if (StrUtil.isBlank(studentSign.getDispatchReportCompanyAddress())) {
            return JsonResult.error("没有找到单位所在地");
        }
        if (StrUtil.isBlank(studentSign.getDispatchCompanyMembership())) {
            return JsonResult.error("没有找到单位隶属");
        }

        return JsonResult.error("没有找到派遣单位信息");
    }

    public void setStudentSignByDispatchCompany(StudentSign studentSign, DispatchCompany dispatchCompany) {
        studentSign.setDispatchReportCompany(dispatchCompany.getCompanyName());
        studentSign.setDispatchArchivesCompany(dispatchCompany.getDispatchCompanyName());
        studentSign.setDispatchArchivesAddress(dispatchCompany.getDispatchAddress());
        studentSign.setDispatchArchivesZip(dispatchCompany.getDispatchZip());
        studentSign.setDispatchArchivesMobile(dispatchCompany.getDispatchMobile());

        studentSign.setDispatchReportCompanyProvince(dictService.cacheValue(DictConstants.TYPE_SIGN_BDZQWDWSSSF, dispatchCompany.getCompanySubjection()));
        studentSign.setDispatchReportCompanyAddress(dictService.cacheValue(DictConstants.TYPE_SYSZD, dispatchCompany.getCompanySignArea()));
        studentSign.setDispatchCompanyMembership(dictService.cacheValue(DictConstants.TYPE_SIGN_DWLS, dispatchCompany.getCompanySubjection()));
    }

}
